<?php
include 'FirePHP.php';
include 'Bmp.php';
// $firebug = FirePHP::getInstance( true );
// $firebug -> log($image);

class LoadData {
	
	public $MAPS_PATH;
	public $POSTRACE_PATH;
	public $choppLogo = 55;
	public function __construct() {
		$this->MAPS_PATH = dirname(dirname(__FILE__))."/maps/";
		$this->POSTRACE_PATH = dirname(dirname(__FILE__))."/potrace/";
	}
	public function getMapTile($latlon){
		if(file_exists($this->MAPS_PATH.$latlon.".png")){
			return file_get_contents($this->MAPS_PATH.$latlon."-mapc.svg");
		}
		$mapImagePath = $this->getImage($latlon);
		if($mapImagePath){
			$imagePath = $this->getImage($latlon,true);
			exec($this->POSTRACE_PATH."potrace.exe -s ".$imagePath, $results);
			$svgFile = str_replace(".bmp", ".svg", $imagePath);
			// for ($second = 0; ; $second++) {
			// if ($second >= 2) return "error";
			// try {
			// if (file_exists($svgFile)) break;
			// } catch (Exception $e) {}
			// sleep(1);
			// }
			return file_get_contents($svgFile);
		} 
	}

	public function getImage($latlon,$collison = false){
		$nameImage = $this->MAPS_PATH.$latlon.".png";
		$scale = 1;
		$URL = "http://maps.googleapis.com/maps/api/staticmap?center=$latlon&zoom=16&size=200x225&scale=2&sensor=false";
		if($collison){
			$URL .= "&style=feature:all%7Chue:0x000000%7Cinvert_lightness:true%7Clightness:00%7Cvisibility:off&style=feature:road|element:geometry%7Cvisibility:on%7Clightness:100";
			$nameImage = $this->MAPS_PATH.$latlon."-mapc.bmp";
			$scale = 1;
		}
		$imgString = file_get_contents($URL);
		$img = imagecreatefromstring($imgString);
		$w = imagesx($img);
		$h = imagesy($img);
		$chopImage = imagecreatetruecolor( $w*$scale, ($h-$this->choppLogo)*$scale);
		imagecopyresampled($chopImage, $img, 0, 0, 0, 0, $w*$scale, $h*$scale, $w, ($h-$this->choppLogo) );	
		if($collison){
			$chopImage = $this->ImageFlip($chopImage,1);
			imagebmp($chopImage, $nameImage, 0);
		}
		else
			imagepng($chopImage, $nameImage, 0);
		imagedestroy($img);
		imagedestroy($chopImage);
		if($img)
			return $nameImage;
		else
			return;
	}
	public function getCollision(){
		//trace
		$path = dirname(dirname(__FILE__)).'\assets\collisionmap.svg';
		$xml = file_get_contents($path);
		//$xml = new SimpleXMLElement($xml);
		
		//$xml = simplexml_load_string($xml);

		//pixel perfect
		// $path = dirname(dirname(__FILE__)).'\assets\collisionmap.png';
		// $image = imagecreatefrompng($path); //imagecreatetruecolor
		// return $this->getParseMapCollision($image);
		//FirePHP
		return $xml;
	}
	
	public function getParseCollision($image){
		$collisionData = '{';
		for ($x=0; $x < imagesx($image); $x++) { 
			for ($y=0; $y < imagesy($image); $y++) { 
				$rgb = imagecolorat($image, $x, $y);
				$color_tran = imagecolorsforindex($image, $rgb);
				$r = ($rgb >> 16) & 0xFF;
				$g = ($rgb >> 8) & 0xFF;
				$b = $rgb & 0xFF;
				if($b > 15)
					$collisionData.='"x'.$x.'.y'.$y.'": "1",';
			}
		}
		return $collisionData.='"end": "1"}';
	}

	function ImageFlip ( $imgsrc, $mode){
    $width = imagesx ( $imgsrc );
    $height = imagesy ( $imgsrc );
    $src_x = 0;
    $src_y = 0;
    $src_width = $width;
    $src_height = $height;

    switch ($mode){
        case '1': //vertical
            $src_y = $height -1;
            $src_height = -$height;
        break;
        case '2': //horizontal
            $src_x = $width -1;
            $src_width = -$width;
        break;
        case '3': //both
            $src_x = $width -1;
            $src_y = $height -1;
            $src_width = -$width;
            $src_height = -$height;
        break;
        default:
            return $imgsrc;
    }
    $imgdest = imagecreatetruecolor ( $width, $height );

    if ( imagecopyresampled ( $imgdest, $imgsrc, 0, 0, $src_x, $src_y , $width, $height, $src_width, $src_height ) )
        return $imgdest;

    return $imgsrc;
	}
	
	function resize_image($file, $w, $h, $crop=FALSE) {
    list($width, $height) = getimagesize($file);
    $r = $width / $height;
    if ($crop) {
        if ($width > $height) {
            $width = ceil($width-($width*($r-$w/$h)));
        } else {
            $height = ceil($height-($height*($r-$w/$h)));
        }
        $newwidth = $w;
        $newheight = $h;
    } else {
        if ($w/$h > $r) {
            $newwidth = $h*$r;
            $newheight = $h;
        } else {
            $newheight = $w/$r;
            $newwidth = $w;
        }
    }
    $src = imagecreatefromjpeg($file);
    $dst = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    return $dst;
	}
}
?>
